package com.poobo.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.poobo.constants.WebConstants;
import com.poobo.entity.TbSysLog;
import com.poobo.service.ISysLogService;
import com.poobo.util.message.AjaxResult;
import com.poobo.util.pager.Page;
import com.poobo.util.str.StrUtil;

@Controller
@RequestMapping("init")
public class InitSysCtr {

	@Autowired
	private ISysLogService iSysLogService;
	
	
	private AjaxResult ajaxResult;
	
	//全局返回内容页
	@RequestMapping("initContext")
	public String initContext(HttpServletRequest request,HttpServletResponse response,TbSysLog sysLog,Page page){
		if(sysLog.getAcitionLoginName()!=null && sysLog.getAcitionLoginName()!=""){
			request.setAttribute("acitionLoginName", sysLog.getAcitionLoginName());
		}if(sysLog.getMthodDesc()!=null && sysLog.getMthodDesc()!=""){
			request.setAttribute("mthodDesc", sysLog.getMthodDesc());
		}
		Page pages=iSysLogService.getTbSysLog(sysLog,page);
		if(pages!=null){
			request.setAttribute("sysLogList",pages.getPageList());  //总数据
			request.setAttribute("pageNo", page.getPageNo());
			request.setAttribute("pageNum", page.getPageNum());  //总页数
			request.setAttribute("pageSize", page.getPageSize());
			request.setAttribute("rowCount", page.getRowCount());
		}
		return "common/frameworkJSP/initContext";
	}
	
	
	//全局返回主页
	@RequestMapping("index")
	public String initIndex(){
		return "index";
	}
	
	//全局初始化登录
	@RequestMapping("login")
	public String toLogin(){
		return "login";
	}
	
	
	public String createImg(HttpServletRequest request) throws Exception {
		String sRand=StrUtil.getRandomNum(10000);
		request.getSession().setAttribute(WebConstants.SYS_LOGIN_CODE, sRand);
		return sRand;
	}
	
	//校验验证码
	@RequestMapping("chLoginCode")
	@ResponseBody
	public String ajaxCkLoginImdCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		String code=request.getParameter("code");
		if(StrUtil.isNotNull(code)){
			if(code.equals(request.getSession().getAttribute(WebConstants.SYS_LOGIN_CODE))){
				return "true";
			}
			return "false";
		}else{
			return "false";
		}
	}
	
	// 获取登录时的验证码
	@RequestMapping("getCode")
	@ResponseBody
	public String ajaxLoginImgCode(HttpServletRequest request) throws Exception {
			String sRand = createImg(request);
			return sRand;
	}
	
	
	// 更换登录时的验证码
	@RequestMapping(value="updateCode",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String ajaxUpdateImgCode(HttpServletRequest request) throws Exception {
			String sRand = createImg(request);
			ajaxResult=new AjaxResult();
			System.out.println(System.currentTimeMillis());
			System.out.println((Long)request.getSession().getAttribute(WebConstants.SYS_LOGIN_CODE_TIME));
			System.out.println(System.currentTimeMillis()-(Long)request.getSession().getAttribute(WebConstants.SYS_LOGIN_CODE_TIME));
			if(System.currentTimeMillis()-(Long)request.getSession().getAttribute(WebConstants.SYS_LOGIN_CODE_TIME)==0){
				ajaxResult.setStatus(false);
				ajaxResult.setMessage("休息一会吧，您操作太快了；");
			}else{
				ajaxResult.setStatus(true);
				ajaxResult.setMessage(sRand);
			}
		return JSON.toJSONString(ajaxResult);
	}
	
	//根据id删除日志
	@RequestMapping("deleteById")
	public String deleteById(HttpServletRequest request){
		Integer id = Integer.parseInt(request.getParameter("logId"));
		iSysLogService.deleteBySysLogId(id);
		return "redirect:/init/initContext.html";
	}
	@RequestMapping("deleteByIds")
	public String deleteByIds(HttpServletRequest request){
		String ids = request.getParameter("ids");
		String[] idss = ids.split(",");
		Integer id = null;
		for (String idStr : idss) {
			id = Integer.parseInt(idStr);
			iSysLogService.deleteBySysLogId(id);
		}
		return "redirect:/init/initContext.html";
	}
}
